#include <stdlib.h>

#include "lib.h"

#include "slotguts.h"
#include "boiguts.h"
#include "slotspin.h"

enum
{
    SGIDX_BANK = 0,
    SG_SZ,
};

enum WheelContents {
    BLANK,
    WILD,
    CHERRY,
    BAR1,
    BAR2,
    BAR3,
    BLUE7,
    RED7,
    PIGGY,
};



enum SpinResult
{
	EVT_LOSER = 0,
	EVT_3PIGGIES,
	EVT_3BLANKS,
	EVT_1CHERRY,
	EVT_1WILD,
	EVT_ANY3BAR,
	EVT_3BAR1,
	EVT_WILDCHERRY,
	EVT_3BAR2,
	EVT_3BAR3,
	EVT_ANY3SEVENS,
	EVT_3BLUESEVENS,
	EVT_3REDSEVENS,
	NUMEVENTS
};

bool8 
BOISlotGuts::isValidGuts()
{
    return getGutsId() == 600 ||
           getGutsId() == 601 ||
           getGutsId() == 602 ||
           getGutsId() == 603 ||
           getGutsId() == 604 ||
           getGutsId() == 605 ||
           getGutsId() == 606 ||
           getGutsId() == 607;
}

void
BOISlotGuts::loadSubGameData()
{
	SlotGuts::loadSubGameData();

	//If the piggie bank is less than the 3 piggie pay, increase it.
	if(m_subGame->getData(SGIDX_BANK) < 3*getPayout(getBetCount(), EVT_3PIGGIES)->payoutAmount)
		 m_subGame->update(SGIDX_BANK, m_subGame->getData(SGIDX_BANK) + 3*getPayout(getBetCount(), EVT_3PIGGIES)->payoutAmount);
}

void
BOISlotGuts::spin(SlotSpin *slotSpin)
{
    SlotGuts::spin(slotSpin);
    SpinLine *spinLine = slotSpin->getSpinLineAt(0);

    if ( spinLine->payouttypeid == EVT_3BLANKS )
    {
        /**
         * Put the bet in the bank.
         */
        m_subGame->update(SGIDX_BANK, slotSpin->getBet() + m_subGame->getData(SGIDX_BANK));
    }
    else if ( spinLine->payouttypeid == EVT_3PIGGIES && slotSpin->getBet() == getBetCount() )
    {
        /**
         * Record the piggie win and reseed the bank.
         */
		///Josh 2/20 - We're paying the seed twice here, so take it out before you give 
		///it to the player. We shortcut the logic by just setting the payout, rather
		///than adding it to the existing pay (which is exactly the reseed).
        spinLine->payoutamount = m_subGame->getData(SGIDX_BANK) * m_info->creditValue;
        m_subGame->update(SGIDX_BANK, 3*getPayout(getBetCount(), EVT_3PIGGIES)->payoutAmount);
    }

}


uint8
BOISlotGuts::classifyStops(uint8* stops,uint8 bet, uint8 line)
{
	uint8 result = EVT_LOSER;
    int numWilds = 0;
    int numCherries = 0;
    int numSevens = 0;
    int numRedSevens = 0;
    int numBlueSevens = 0;
    int numBar1 = 0;
    int numBar2 = 0;
    int numBar3 = 0;
    int numBars = 0;
    int numBlanks = 0;
    int numPiggies = 0;

    //  Do a preliminary run-through
    for ( uint8 i = 0; i < getReelCount(); i++ )
    {
        // Set the icon flags;
        switch ( getIcon(i, stops[i])->iconid )
        {
            case BLANK:
                numBlanks++;
                break;

            case WILD:
                numWilds++;
                break;

            case CHERRY:
                numCherries++;
                break;

            case RED7:
                numRedSevens++;
                numSevens++;
                break;

            case BLUE7:
                numBlueSevens++;
                numSevens++;
                break;

            case BAR1:
                numBar1++;
                numBars++;
                break;

            case BAR2:
                numBar2++;
                numBars++;
                break;

            case BAR3:
                numBar3++;
                numBars++;
                break;

            case PIGGY:
                numPiggies++;
                break;
        }
    }

    //  EVT_3BLANKS
    if ( numBlanks == 3 )
    {
        result = EVT_3BLANKS;
    }

    //  EVT_3PIGGIES
    if ( numPiggies + numWilds == 3 )
    {
        result = EVT_3PIGGIES;
    }

    // EVT_3BAR1
    if ( result == EVT_LOSER && numBar1 + numWilds == 3 )
    {
        result = EVT_3BAR1;
    }

    // EVT_3BAR2
    if ( result == EVT_LOSER && numBar2 + numWilds == 3 )
    {
        result = EVT_3BAR2;
    }

    // EVT_3BAR3
    if ( result == EVT_LOSER && numBar3 + numWilds == 3 )
    {
        result = EVT_3BAR3;
    }

    // EVT_ANY3BARS     -  Must be checked after the bar triplets
    if ( result == EVT_LOSER && numBars + numWilds == 3 )
    {
        result = EVT_ANY3BAR;
    }

    // EVT_3BLUESEVENS
    if ( result == EVT_LOSER && numBlueSevens + numWilds == 3 )
    {
        result = EVT_3BLUESEVENS;
    }

    // EVT_3REDSEVENS       ** Jackpot ** 
    if ( result == EVT_LOSER && numRedSevens + numWilds == 3 )
    {
        result = EVT_3REDSEVENS;
    }

    // EVT_ANY3SEVENS
    if ( result == EVT_LOSER && numSevens + numWilds == 3 )
    {
        result = EVT_ANY3SEVENS;
    }


    // EVT1WILDCHERRY
    //
    //  **  This has to be after basic basic results so the wild card
    //      matches can be found first.
    if ( result == EVT_LOSER && numWilds == 1 && numCherries == 1 )
    {
        result = EVT_WILDCHERRY;
    }

    // EVT1WILD
    //
    //  **  This has to be after basic basic results so the wild card
    //      matches can be found first.
    if ( result == EVT_LOSER && numWilds == 1 )
    {
        result = EVT_1WILD;
    }

    // EVT1WILDCHERRY
    //
    //  **  This has to be after basic basic results so the wild card
    //      matches can be found first.
    if ( result == EVT_LOSER && numCherries == 1 )
    {
        result = EVT_1CHERRY;
    }


	return result;
}


